/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Renderers;

import comu.Configuracio;
import entity.Abono;
import entity.Camp;
import entity.Collita;
import entity.Fitosanitari;
import entity.ModeAplicacio;
import entity.Produccio;
import entity.Producte;
import entity.Productor;
import entity.UnitatsMesura;
import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import vistas.textImg.Textos;

/**
 *
 * @author David
 */
public class LabelRenderer extends JLabel implements ListCellRenderer {

    public LabelRenderer() {
        setOpaque(true);
        setHorizontalAlignment(LEFT);
        setVerticalAlignment(CENTER);
        setFont(Textos.FONT_GRAN);
    }

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        if (Producte.class.isInstance(value)) {
            Producte producte = (Producte) value;
            if ((producte.getImg() != null) && (!producte.getImg().equals(""))) {
                try {
                    int with = Configuracio.SICE_IMG_PRODUCTE_H;
                    int height = Configuracio.SICE_IMG_PRODUCTE_V;
                    ByteArrayInputStream bais = new ByteArrayInputStream(producte.getImg());
                    BufferedImage src = ImageIO.read(bais);
                    Image scaledImage = src.getScaledInstance(with, height, Image.SCALE_SMOOTH);
                    BufferedImage imgBuff = new BufferedImage(with, height, BufferedImage.TYPE_INT_RGB);
                    imgBuff.getGraphics().drawImage(scaledImage, 0, 0, new Color(0, 0, 0), null);
                    setIcon(new ImageIcon(imgBuff));

                } catch (IOException ex) {
                    Logger.getLogger(LabelRenderer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            setText(producte.getNom().toUpperCase());
        } else if (Camp.class.isInstance(value)) {
            Camp camp = (Camp) value;
            setText(camp.getNom().toUpperCase());
        } else if (Produccio.class.isInstance(value)) {
            Produccio produccio = (Produccio) value;
            setText(Configuracio.sdf.format(produccio.getDataPlanta()) + " " + produccio.getProductesId().getNom());
        } else if (Collita.class.isInstance(value)) {
            Collita collita = (Collita) value;
            setText(Configuracio.sdf.format(collita.getData()) + " " + collita.getQuantitat() + " " + collita.getUnitatsMesuraId());
        } else if (Productor.class.isInstance(value)) {
            Productor productor = (Productor) value;
            setText(productor.getNom());
        } else if (UnitatsMesura.class.isInstance(value)) {
            UnitatsMesura unitat = (UnitatsMesura) value;
            setText(unitat.getNom());
        } else if (Abono.class.isInstance(value)) {
            Abono abono = (Abono) value;
            if ((abono.getImg() != null) && (!abono.getImg().equals(""))) {
                try {
                    int with = Configuracio.SICE_IMG_PRODUCTE_H;
                    int height = Configuracio.SICE_IMG_PRODUCTE_V;
                    ByteArrayInputStream bais = new ByteArrayInputStream(abono.getImg());
                    BufferedImage src = ImageIO.read(bais);
                    Image scaledImage = src.getScaledInstance(with, height, Image.SCALE_SMOOTH);
                    BufferedImage imgBuff = new BufferedImage(with, height, BufferedImage.TYPE_INT_RGB);
                    imgBuff.getGraphics().drawImage(scaledImage, 0, 0, new Color(0, 0, 0), null);
                    setIcon(new ImageIcon(imgBuff));

                } catch (IOException ex) {
                    Logger.getLogger(LabelRenderer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            setText(abono.getNom().toUpperCase() + "(" + abono.getQuantitatPaquet() + " " + abono.getUnitatsQuantitat().getNom() + ")");
        } else if (Fitosanitari.class.isInstance(value)) {
            Fitosanitari fitos = (Fitosanitari) value;
            if ((fitos.getImg() != null) && (!fitos.getImg().equals(""))) {
                try {
                    int with = Configuracio.SICE_IMG_PRODUCTE_H;
                    int height = Configuracio.SICE_IMG_PRODUCTE_V;
                    ByteArrayInputStream bais = new ByteArrayInputStream(fitos.getImg());
                    BufferedImage src = ImageIO.read(bais);
                    Image scaledImage = src.getScaledInstance(with, height, Image.SCALE_SMOOTH);
                    BufferedImage imgBuff = new BufferedImage(with, height, BufferedImage.TYPE_INT_RGB);
                    imgBuff.getGraphics().drawImage(scaledImage, 0, 0, new Color(0, 0, 0), null);
                    setIcon(new ImageIcon(imgBuff));

                } catch (IOException ex) {
                    Logger.getLogger(LabelRenderer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            setText(fitos.getNomComercial().toUpperCase());
        } else if (ModeAplicacio.class.isInstance(value)) {
            ModeAplicacio mode = (ModeAplicacio) value;
            setText(mode.getNom());
        }
        return this;
    }
}